<template>
  <div>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <el-form ref="form" :model="form"  status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
    <el-form-item label="原密码" prop="old_pwd">
      <el-input type="password" v-model="form.old_pwd" :style="{ width: '400px' }" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="new_pwd">
      <el-input type="password" v-model="form.new_pwd" :style="{ width: '400px' }" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认新密码" prop="re_pwd">
      <el-input type="password" v-model="form.re_pwd" :style="{ width: '400px' }"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onsubmit">提交修改</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
</el-form>
  </el-card>
</div>
</template>

<script>
import { resetPwdAPI } from '@/api/user'
export default {
  name: 'user-pwd',
  data () {
    const checkRePwd = (rule, value, cb) => {
      if (value !== this.form.new_pwd) {
        cb(new Error('两次新密码不一致！'))
      } else {
        cb()
      }
    }
    return {
      form: {
        ...this.defaultForm
      },
      rules: {
        old_pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度必须是6-15位的非空字符串', trigger: ['blur'] }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度必须是6-15位的非空字符串', trigger: ['blur'] }
        ],
        re_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度必须是6-15位的非空字符串', trigger: ['blur'] },
          { validator: checkRePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onsubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const res = await resetPwdAPI(this.form)
          if (res.code === 0) {
            this.$message.success('更新成功')
          } else {
            this.$message.error('更新密码失败')
          }
        }
      })
    },
    resetForm () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
